<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Produk extends CI_Controller {
	
	function __construct() {
        parent::__construct();	
		$this->isLogin();	
		$this->path = './foto_produk/';
		$this->load->model('produk_model');
		$this->num_post=10;
	}
	
	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}

	public function index($offset = 0)
	{
		$data['aksi_cari']='admin_produk/cari_produk';
		$data['main_view']='produk/main';
		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		
		$num_post=10;
		
		$data['hasil']=$this->produk_model->getData($num_post, $offset, "");
		foreach($data['hasil'] as $key=>$val){
			$cek_produk=$this->produk_model->cek_produk($val['id_produk']);
			if($cek_produk==true){
				$status="ya";
			}else{
				$status="tidak";
			}
			$data['hasil'][$key]['status']=$status;
		}
		foreach($data['hasil'] as $val){
			$data['parent_menu'][$val['id_produk']]=$this->produk_model->get_parent($val['id_parent']);
		}
		$data['offset']=$offset;
		$num_rows = $this->produk_model->count_all_num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'admin_produk/index';//site_url('');
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('admin_template',$data);
	}
	
	//cari data
	public function cari_produk($offset = 0)
	{
		$data['aksi_cari']='admin_produk/cari_produk';
		$data['main_view']='produk/main';
		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		//buat cari
		if(isset($_POST['cari'])){
			$data['param_judul'] = $this->input->post('judul');
			$this->session->set_userdata('param_judul', $data['param_judul']);
		} else {
			$data['param_judul'] = $this->session->userdata('param_judul');
		}
		
		$num_post=10;
		
		$data['hasil']=$this->produk_model->getData($this->num_post, $offset, $data['param_judul']);
		foreach($data['hasil'] as $key=>$val){
			$cek_produk=$this->produk_model->cek_produk($val['id_produk']);
			if($cek_produk==true){
				$status="ya";
			}else{
				$status="tidak";
			}
			$data['hasil'][$key]['status']=$status;
		}
		foreach($data['hasil'] as $val){
			$data['parent_menu'][$val['id_produk']]=$this->produk_model->get_parent($val['id_parent']);
		}
		$data['offset']=$offset;
		$num_rows = $this->produk_model->count_all_num_rows($data['param_judul']);
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'admin_produk/index';//site_url('');
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('admin_template',$data);
	}
	
	function view_add(){
		$data['main_view']='produk/view_add';
		$parent_menu=$this->produk_model->getParentData();
		$data['list_parent'][0]='Parent Menu';
		foreach($parent_menu as $row){
			$data['list_parent'][$row['id_produk']]=$row['nama_menu'];
		}
		$this->load->view('admin_template',$data);
	}
	
	function proses_add(){
		$judul=$this->input->post('judul');
		$tags=$this->input->post('tags');
		$konten=$this->input->post('konten');
		$tmp_gambar=$this->input->post('gambar');
		$nama_menu=$this->input->post('nama_menu');
		$deskripsi=$this->input->post('deskripsi');
		$parent_menu=$this->input->post('parent_menu');
		$slug=$this->produk_model->create_slug($judul);
		if(!empty($_FILES['gambar']['name'])){
			$config = array(
 				'allowed_types'=>'jpg|jpeg|gif|png',
 				'upload_path' => $this->path,
 				'max_size' => 2000);

			$this->load->library('upload',$config);
			$status_upload=$this->upload->do_upload('gambar');
 			$image_data = $this->upload->data();
			//resize gambar
			$config = array(
 					'source_image'=> $image_data['full_path'],
 					'new_image'=>$this->path . '/thumbs',
 					'maintain_ration'=>true,
 					'width'=>160,
 					'height'=>120);
			$this->load->library('image_lib', $config);
	 		$this->image_lib->resize();
			//print_r($image_data);
			$nama_gambar=$image_data['file_name'];
		}else{
			$nama_gambar="";
			
		}
		
		if($query=$this->produk_model->insert_data($judul,$nama_gambar,$slug,$konten,$parent_menu,$nama_menu,$deskripsi,$tags)){
			$this->session->set_flashdata('pesan', 'Data Sukses Ditambah');
			redirect('admin_produk');
			
		}
	}
	
	function view_edit($id){
		$data['main_view']='produk/view_edit';
		$data['hasil']=$this->produk_model->get_detail_data($id);
		$parent_menu=$this->produk_model->getParentData();
		$data['list_parent'][0]='Parent Menu';
		foreach($parent_menu as $row){
			$data['list_parent'][$row['id_produk']]=$row['nama_menu'];
		}
		$this->load->view('admin_template',$data);
	}
	
	function proses_update(){
		$judul=$this->input->post('judul');
		$tags=$this->input->post('tags');
		$konten=$this->input->post('konten');
		$id=$this->input->post('id');
		$nama_menu=$this->input->post('nama_menu');
		$deskripsi=$this->input->post('deskripsi');
		$parent_menu=$this->input->post('parent_menu');
		$tmp_gambar=$this->input->post('tmp_gambar');
		$tmp_slug=strtolower($this->input->post('tmp_slug'));
		if(strtolower(url_title($judul)) != $tmp_slug){
			$slug=$this->produk_model->create_slug($judul);
		}else{
			$slug=$tmp_slug;
		}
		if(!empty($_FILES['gambar']['name'])){
			$config = array(
 				'allowed_types'=>'jpg|jpeg|gif|png',
 				'upload_path' => $this->path,
 				'max_size' => 2000);

			$this->load->library('upload',$config);
			$status_upload=$this->upload->do_upload('gambar');
 			$image_data = $this->upload->data();
			//resize gambar
			$config = array(
 					'source_image'=> $image_data['full_path'],
 					'new_image'=>$this->path . '/thumbs',
 					'maintain_ration'=>true,
 					'width'=>160,
 					'height'=>120);
			$this->load->library('image_lib', $config);
	 		$this->image_lib->resize();
			//hapus gambar lama
			$hapus=@unlink($this->path.$tmp_gambar);
			$hapus=@unlink($this->path.'/thumbs/'.$tmp_gambar);
			//print_r($image_data);
			$nama_gambar=$image_data['file_name'];
		}else{
			$nama_gambar=$tmp_gambar;
			
		}
		
		if($query=$this->produk_model->update_data($id,$judul,$nama_gambar,$konten,$parent_menu,$nama_menu,$slug,$deskripsi,$tags)){
			$this->session->set_flashdata('pesan', 'Data Sukses Ditambah');
			redirect('admin_produk');
			
		}
	}
	
	function delete($id){
		$detail=$this->produk_model->get_detail_data($id);
		if($detail['gambar']!=""){
			//hapus file
			$hapus=@unlink($this->path.$detail['gambar']);
			$hapus=@unlink($this->path.'/thumbs/'.$detail['gambar']);
			//echo $this->path.$detail['gambar'];
			//var_dump($hapus);
			//exit;
		}
		$this->produk_model->delete_data($id);
		$this->session->set_flashdata('pesan', 'Data Sukses Dihapus');
		redirect('admin_produk');
	}
}